<?php

class SalesManager extends BaseManager implements iManager {

	public function query(){
		$query = "SELECT * FROM `sales_tbl` WHERE sales_id = " . $this->obj->getSalesId();
	}
	
	public function add() {
		$data['product_total_count'] = $this->obj->getTotalQty();
		$data['total_amount'] = $this->obj->getTotalAmount();
		$data['date_sold'] = $this->obj->getDateSold();
		$data['sold_by'] = $this->obj->getEmployee()->getEmployeeId();
		
		$this->db->insert('sales_tbl', $data);
		
		$this->db->execute();
		
		$sales = new Sales_Domain();
		
		$sales->setSalesId($this->db->getLastId());
		
		$this->result = $sales;
	}
	
	public function update(){}
	
	public function delete(){}
	
	public function getTodaySales() {
		$return = array();
		
		$query = "SELECT 
				    sales_id, product_total_count, total_amount,
				    date_sold, firstname, lastname
				FROM `sales_tbl` AS s
				LEFT JOIN `employee_tbl` AS e
				    ON e.employee_id = s.sold_by
				WHERE date_sold BETWEEN SUBDATE(NOW(), INTERVAL 24 HOUR) AND NOW();";
		
		$this->db->query($query);
		
		$this->db->execute();
		
		while ($result = $this->db->getResultAssoc()) {
			$sales = new Sales_Domain();
			
			$sales->setSalesId($result['sales_id']);
			$sales->setTotalAmount($result['total_amount']);
			$sales->setTotalQty($result['product_total_count']);
			
			$employee = new Employee_Domain();
			
			$employee->setFirstName($result['firstname']);
			$employee->setLastName($result['lastname']);
			
			$sales->setEmployee($employee);
			
			
			$return[] = $sales;
		}
		
		$this->result = $return;
	} 
}